<template>
   <ul class="letterList">
      <li
        class="item"
        v-for="item in arrLetter"
        :key="item"
        :ref="item"
        @click="handleClick"
        @touchstart="touchStart"
        @touchmove="touchMove"
        @touchend="touchEnd"
      >
        {{item}}
      </li>
   </ul>
</template>

<script>
export default {
  name: 'letterList',
  props: {
    city: Object
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  computed: {
    arrLetter () {
      const arrList = []
      for (let i in this.city) {
        arrList.push(i)
      }
      return arrList
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleClick (e) {
      // console.log(e.target.innerText)
      this.$emit('change', e.target.innerText)
    },
    touchStart () {
      this.touchStatus = true
    },
    touchMove (e) {
      if (this.touchStatus) {
        // const startY = this.$refs['A'][0].offsetTop  //这块是个固定的值,不能哪次都计算需要优化
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 89
          const index = Math.floor((touchY - this.startY) / 20)
          // console.log(index)
          if (index >= 0 && index < this.arrLetter.length) {
            this.$emit('change', this.arrLetter[index])
          }
        }, 5)
      }
    },
    touchEnd () {
      this.touchStatus = false
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~style/varibles.styl'
.letterList
  display:flex
  flex-direction:column
  justify-content:center
  position:absolute
  top:1.7rem
  right:0
  bottom:0
  width:0.4rem
  .item
    line-height:0.35rem
    text-align:center
    color:$bgColor
    font-size:.25rem
</style>
